app.controller('clientCtrl',['$scope','toaster','utilService','clientService','$modal',
    function($scope,toaster,utilService,clientService,$modal){

    $scope.client={};
    $scope.query = function(){
        if($scope.client.memberCardId==''){
            delete $scope.client.memberCardId;
        }
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'client',
            params:$scope.client
        }).then(function(result){
        });
    };
    $scope.query();

    //查找会员卡
    $scope.memberCardVO={};
    $scope.getMemberCardName = function(){
        clientService.getMemberCardAll($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.memberCardVO = result.data;
                    break;
                case 666:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };
    $scope.getMemberCardName();

    //升级为会员
    $scope.upgrade = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/member/client_upgrade.html',
            controller: 'upgradeCtrl',
            size: 'sm',
            scope: $scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });
    };

    //修改会员卡信息
    $scope.update = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/member/client_update.html',
            controller: 'updateCtrl',
            size: 'sm',
            scope: $scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });
    };

    //在线储值会员卡
    $scope.charge = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/member/client_charge.html',
            controller: 'chargeCtrl',
            size: 'sm',
            scope: $scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });
    };

    //预留店铺会员编号
    $scope.reserved = function () {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/member/client_reserved.html',
            controller: 'reservedCtrl',
            size: 'sm',
            scope: $scope
        });
    };

    //赠送优惠券
    $scope.giveCoupons = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/member/client_give_coupons.html',
            controller: 'giveCouponsCtrl',
            size: 'sm',
            scope: $scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });
    };
}]);

//升级为会员模态框
app.controller('upgradeCtrl', ['$scope', '$modalInstance', 'clientService', 'toaster', 'data',
    function ($scope, $modalInstance, clientService, toaster, data) {
        $scope.memberClient ={
            id:data
        };
        $scope.clientUpdate={
            id:data
        };
        $scope.isTrue = true;

        $scope.confirm = function () {
            if (!$scope.isTrue) {
                return;
            }
            $scope.isTrue = false;
            clientService.confirm($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "操作", "操作成功！");
                        $modalInstance.dismiss('success');
                        $scope.query();
                        $scope.isTrue = true;
                        break;
                    case 666:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该会员卡,请刷新重试!");
                        $scope.isTrue = true;
                        break;
                    case 720:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "您还没预留该会员编号，无法添加该会员！");
                        $scope.isTrue = true;
                        break;
                    case 719:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "该会员编号已超出最大会员编号,请更换会员编号!");
                        $scope.isTrue = true;
                        break;
                    case 718:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "该会员编号已存在,请更换会员编号!");
                        $scope.isTrue = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        //查找该用户的信息
        $scope.findClientById = function(){
            clientService.findClientById($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.clientUpdate = result.data;
                        break;
                    case 667:
                        toaster.pop('error', "操作失败", "找不到该客户!请刷新重试");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findClientById();

        //查找会员卡
        $scope.memberCardVO={};
        $scope.getMemberCardName = function(){
            clientService.getMemberCardAll($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.memberCardVO = result.data;
                        break;
                    case 666:
                        toaster.pop('waiting', "提示", result.message);
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.getMemberCardName();
    }
]);

//修改会员卡的模态框
app.controller('updateCtrl', ['$scope', '$modalInstance', 'clientService', 'toaster', 'data',
    function ($scope, $modalInstance, clientService, toaster, data) {
        $scope.clientUpdate ={
            id:data
        };
        $scope.isTrue = true;

        $scope.updateCard = function () {
            if (!$scope.isTrue) {
                return;
            }
            $scope.isTrue = false;
            clientService.updateCard($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "操作", "操作成功！");
                        $modalInstance.dismiss('success');
                        $scope.query();
                        $scope.isTrue = true;
                        break;
                    case 666:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该会员卡,请刷新重试!");
                        $scope.isTrue = true;
                        break;
                    case 667:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该客户,请刷新重试!");
                        $scope.isTrue = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        //查找该用户的信息
        $scope.findClientById = function(){
            clientService.findClientById($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.clientUpdate = result.data;
                        break;
                    case 667:
                        toaster.pop('error', "操作失败", "找不到该客户!请刷新重试");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findClientById();

        //查找会员卡
        $scope.memberCardVO={};
        $scope.getMemberCardName = function(){
            clientService.getMemberCardAll($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.memberCardVO = result.data;
                        break;
                    case 666:
                        toaster.pop('waiting', "提示", result.message);
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.getMemberCardName();
    }
]);

//在线储值模态框
app.controller('chargeCtrl', ['$scope', '$modalInstance', 'clientService', 'toaster', 'data',
    function ($scope, $modalInstance, clientService, toaster, data) {
        $scope.clientCharge ={
            id:data
        };
        $scope.isTrue = true;

        $scope.charge = function () {
            if (!$scope.isTrue) {
                return;
            }
            $scope.isTrue = false;
            clientService.charge($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "操作", "操作成功！");
                        $modalInstance.dismiss('success');
                        $scope.query();
                        $scope.isTrue = true;
                        break;
                    case 667:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该会员,请刷新重试！");
                        $scope.isTrue = true;
                        break;
                    case 721:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "该优惠劵已经领取过了!");
                        $scope.isTrue = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        //获取该商家优惠券
        $scope.coupons={};
        $scope.query = function() {
            clientService.findCoupons($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.couponsList = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.query();

    }
]);


//预留会员编号模态框
app.controller('reservedCtrl', ['$scope', '$modalInstance', 'clientService', 'toaster',
    function ($scope, $modalInstance, clientService, toaster) {
        $scope.reservedNumber={};
        $scope.isTrue = true;

        $scope.reservedNum = function () {
            if (!$scope.isTrue) {
                return;
            }
            $scope.isTrue = false;
            clientService.reservedNum($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $modalInstance.dismiss('success');
                        $scope.isTrue = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };



        //查找最大值及取值范围
        $scope.findValue = function(){
            clientService.findValue($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.value = result.data;
                        if($scope.value==null){
                            $scope.reservedNumber.start=1;
                        }else{
                            $scope.reservedNumber.start=parseInt($scope.value.maxValue);
                        }
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.findValue();
    }
]);


//赠送优惠券
app.controller('giveCouponsCtrl', ['$scope', '$modalInstance', 'clientService', 'toaster', 'data',
    function ($scope, $modalInstance, clientService, toaster, data) {
        $scope.coupons ={
            id:data
        };
        $scope.isTrue = true;

        $scope.give = function () {
            if (!$scope.isTrue) {
                return;
            }
            $scope.isTrue = false;
            clientService.give($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "操作", "操作成功！");
                        $modalInstance.dismiss('success');
                        $scope.query();
                        $scope.isTrue = true;
                        break;
                    case 721:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "该优惠劵已经领取过了!");
                        $scope.isTrue = true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

        //获取该商家优惠券
        $scope.coupons={};
        $scope.query = function() {
            clientService.findCoupons($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.couponsList = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        };
        $scope.query();

    }
]);